<?php
namespace app\shop\controller;
use think\Db;
use think\paginator\driver\Bootstrap;
class Scenic extends Base{
    public function scenic(){
        if(!empty(input("param.lat"))){
            $latitude=input("param.lat");
        }else{
            $latitude='30.57859';
        }
        if(!empty(input("param.lng"))){
            $longitude=input("param.lng");
        }else{
            $longitude='114.27803';
        }
        $c_id=input('get.c_id');
        $flag_type=input('get.flag_type');
        $pageNumber = input('get.page')?input('get.page'):'1';
        if($pageNumber>0){
            $pageNumber_one=$pageNumber-1;
        }else{
            $pageNumber_one=0;
        }
        $limit=6;
        $offset=$pageNumber_one*$limit;

        $keyword=input('get.keyword');
        $sort_type=input('get.sort_type');
        
        if($sort_type==1){
            $orderby='sales desc';
        }elseif($sort_type==2){
            $orderby='sales asc';
        }else{
            $orderby = 'id desc';
        }
        $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
    	if(input("param.flag_type")==1){//优选景区
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by ".$orderby.") as a where a.distance<=5000000 limit ".$offset.",".$limit;
            $sqls="select count(*) as count_num from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by ".$orderby.") as a where a.distance<=5000000";
        }elseif(input("param.flag_type")==2){//免费景区
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword and price=0 order by ".$orderby.", id desc) as a where a.distance<=5000000 limit ".$offset.",".$limit;
            $sqls="select count(*) as count_num from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword and price=0 order by ".$orderby.", id desc) as a where a.distance<=5000000";
        }elseif(input("param.flag_type")==3){//距离近->远
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by distance".$orderby.") as a where a.distance<=5000000 limit ".$offset.",".$limit;
            $sqls="select count(*) as count_num from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by distance".$orderby.") as a where a.distance<=5000000";
        }else{//全部
            $sql="select * from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by ".$orderby.") as a where a.distance<=5000000 limit ".$offset.",".$limit;
            $sqls="select count(*) as count_num from(select id,title,img,price,address,state,stars,ROUND(6378.138*2*ASIN(SQRT(POW(SIN(($latitude*PI()/180-`latitude`*PI()/180)/2),2)+COS($latitude*PI()/180)*COS(`latitude`*PI()/180)*POW(SIN(($longitude*PI()/180-`longitude`*PI()/180)/2),2)))*1000) AS distance from khdz_scenic_spot where state < 3 and title LIKE :keyword order by ".$orderby.") as a where a.distance<=5000000";
        }
        $spots=Db::query($sql,['keyword'=>'%'.$keyword.'%']);
        $counts=Db::query($sqls,['keyword'=>'%'.$keyword.'%']);
        $count=$counts['0']['count_num'];
        $pagernator=Bootstrap::make($spots,$limit,$pageNumber,$count,false,['path'=>Bootstrap::getCurrentPath(),'query'=>request()->param()]);



        $page = $pagernator->render();

        $this->assign('cates',$cates);
        $this->assign('c_id',$c_id);
        $this->assign('flag_type',$flag_type);

        $this->assign('spots',$spots);
        $this->assign('page', $page);
        $this->assign('keyword',$keyword);
        $this->assign('sort_type',$sort_type);
        $this->assign('url',$this->request->domain());
        $this->assign('system',$this->system_set());
        return $this->fetch();
    }
    public function scenic_detail(){
        $id=input("param.id");
        $url=$this->request->domain();
        $cates=Db::name('cates')->where('f_id',0)->where('state',1)->select();
        $spotDetail=Db::name('scenic_spot')->where('id',$id)->find();
        $productNew=Db::name('scenic_spot')->where('state',1)->order('id desc')->limit(2)->select();
        if($spotDetail){
            $spotDetail['imgs']=explode(',',$spotDetail['imgs']);
        }
        $this->assign('spotDetail',$spotDetail);
        $this->assign('cates',$cates);
        $this->assign('url',$url);
        $this->assign('id',$id);
        $this->assign('productNew',$productNew);
        $this->assign('system',$this->system_set());
        return $this->fetch();
    }
    public function evaluates()
    {
        if(!empty(input("param.p_id"))){
            $p_id=input("param.p_id");
        }else{
            return json(['code' => 501, 'msg' => '产品或门票id']);
        }
        if(!empty(input("param.page"))){
            $page=input("param.page");
        }else{
            return json(['code' => 502, 'msg' => '当前分页数']);
        }
        if(input("param.type")==1||input("param.type")==2){
            $data['type']=input("param.type");
        }else{
            return json(['code' => 503, 'msg' => '产品还是门票']);
        }
        $res=Db::name('evaluates')->alias('a')->join('users b', 'a.user_id = b.id','LEFT')->field('a.*,b.headimgurl,b.nickname,b.phone')->where('a.p_id',$p_id)->where('a.state',1)->where('a.type',$data['type'])->order('a.id desc')->limit(10*($page-1),10)->select();
        if ($res) {
            foreach ($res as $key => $value) {
                $res[$key]['phone'] = substr_replace($res[$key]['phone'], '****', 3, 4);
                $res[$key]['time'] = date('Y-m-d H:i:s',$res[$key]['time']);
            }
            return json(['code' => 200, 'msg' => '成功！','data'=>$res]);
        }else{
            return json(['code' => 500, 'msg' => '查无数据！']);
        }
    }
}
